package applications.camoverlay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

public class CameraActivity extends Activity {
	static final int MENU_OVERLAY_CHOICE=0, MENU_TRANSPARENCY=1;
	static final int DIALOG_TRANSPARENCY=0;
	
	static SharedPreferences settings;
	static SharedPreferences.Editor editor;
	static final String PREFS_NAME = "prefs";
	
	private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/";
	
	private Resources res;
	public static ImageView iv;
	Camera.PictureCallback jpeg;
	
	MediaPlayer.OnCompletionListener compl=new MediaPlayer.OnCompletionListener (){
		public void onCompletion(MediaPlayer mp) {
			mp.release();
		}
	};
	
	Camera.AutoFocusCallback AFCallback = new Camera.AutoFocusCallback() {
		
		public void onAutoFocus(boolean success, Camera camera) {
			MediaPlayer mp=MediaPlayer.create(getApplicationContext(), R.raw.focused);
    		mp.setOnCompletionListener(compl);
    		mp.start();
		}
	};
	
	SeekBar.OnSeekBarChangeListener sbListener=new SeekBar.OnSeekBarChangeListener(){

		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			iv.setAlpha(progress);
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
		
	};
	
	View.OnClickListener okListener = new View.OnClickListener() {
		public void onClick(View v) {
			dismissDialog(DIALOG_TRANSPARENCY);
		}
	};
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        jpeg=new PictureCallback();
        
        iv=(ImageView)findViewById(R.id.overlayview);
        iv.setAlpha(100);
        
        settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
	    editor   = settings.edit();
        res=getResources();
        
        Bitmap b=BitmapFactory.decodeFile(path+settings.getString("last", ""));
        if(b!=null)
        	iv.setImageBitmap(b);
        
    }
    
    public boolean onTouchEvent(MotionEvent e){
    	
    	if(e.getAction()!=MotionEvent.ACTION_UP)
    		return false;
    	
    	if(iv.getVisibility()==View.VISIBLE)
			iv.setVisibility(View.INVISIBLE);
		else if (iv.getVisibility()==View.INVISIBLE)
			iv.setVisibility(View.VISIBLE);
    	
    	return true;
    	
    }
    
    public void onResume(){
    	super.onResume();
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event){
    	if(event.getKeyCode()==KeyEvent.KEYCODE_CAMERA){
    		MediaPlayer mp=MediaPlayer.create(getApplicationContext(), R.raw.click);
    		mp.setOnCompletionListener(compl);
    		mp.start();
    		
    		SurfaceCallback.camera.takePicture(null, null, jpeg);
    		
    		return true;
    	}
    	
    	if(event.getKeyCode()==KeyEvent.KEYCODE_FOCUS){
    		
    		SurfaceCallback.camera.autoFocus(AFCallback);
    		
    		return true;
    	}
    	
    	
    	
    	return false;
    }
    
    public boolean onCreateOptionsMenu (Menu menu){
    	
    	menu.add(Menu.NONE, MENU_TRANSPARENCY, 2, res.getString(R.string.transparency));
    	menu.add(Menu.NONE, MENU_OVERLAY_CHOICE, 3, res.getString(R.string.chooseoverlay));
    	
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i=new Intent();
        switch (item.getItemId()) {
        	
        	case MENU_OVERLAY_CHOICE:
        		i.setClass(getApplicationContext(), GalleryActivity.class);
        		startActivity(i);
        		break;
        		
        	case MENU_TRANSPARENCY:
        		showDialog(DIALOG_TRANSPARENCY);
        		break;
        		
        }
        return true;
    }
    
    protected Dialog onCreateDialog(int id) {
        Dialog dialog=null;
        switch(id) {
        	
        case DIALOG_TRANSPARENCY:
        	dialog=new Dialog(this);
        	dialog.setContentView(R.layout.transp_dialog);
        	dialog.setTitle(R.string.tvalue);
        	SeekBar sb=(SeekBar)dialog.findViewById(R.id.tSeekBar);
        	dialog.findViewById(R.id.okBtn).setOnClickListener(okListener);
        	sb.setOnSeekBarChangeListener(sbListener);
    		break;
    		
        }
        return dialog;
    }

    
}